<?php

namespace app\db\model;

use think\Db;
use think\Model;

class DbUserRelationDbChild extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';
    protected $dbName     = 'db_user_relation_db_child';

    # --------------------------------- 查询类接口 START------------------------------------
    /**
     * 获取医务详情信息
     * @param int  $usercare_id 数据库ID
     * @author MSQ 2021-8-2
    **/
    public function getDetails($usercare_id = 0,$field = '*')
    {
        if ($field && $field != '*' && count(explode(',', $field)) == 1) {
            // 只取一个字段
            $info = $this->where(['id' => $usercare_id])->value($field);
        }else{
            $info = db($this->dbName)->field($field)->where(['id' => $usercare_id])->find();
        }
        
        return $info;
    }
    /**
     * 获取复制信息
     * 可用于判断用户是否复制
     * @param int  $origin_db_id 总库 已去除
     *             $goal_db_id 子库id
     *             $$user_ids 用户id
     * @author MSQ 2021-8-5
    **/
    public function getCopyUsers($goal_db_id = 0,$user_ids = [],$field = '*')
    {
        if (!$goal_db_id) return false;
        $where = [
            'status'       => 1,
            // 'origin_db_id' => $origin_db_id,
            'goal_db_id'   => $goal_db_id,
        ];
        $isOnlyField = $field && $field != '*' && count(explode(',', $field)) == 1;
        if ($user_ids) {
            if (is_array($user_ids)) {
                $where['user_id'] = ['IN',$user_ids];

                if ($isOnlyField) {
                    // 只取一个字段
                    $info = db($this->dbName)->field($field)->where($where)->column('user_id,'.$field);
                }else{
                    $info = db($this->dbName)->field($field)->where($where)->select();
                }
            }else{
                $where['user_id'] = $user_ids;

                if ($isOnlyField) {
                    // 只取一个字段
                    $info = $this->where($where)->value($field);
                }else{
                    $info = db($this->dbName)->field($field)->where($where)->find();
                }
            }
        }else{
            # 未指定用户 返回所有复制的病例
            if ($isOnlyField) {
                // 只取一个字段
                $info = db($this->dbName)->field($field)->where($where)->column('user_id,'.$field);
            }else{
                $info = db($this->dbName)->field($field)->where($where)->select();
            }
        }
        if (!$info) return [];
        return $info;
    }
    # ---------------------------------- 查询类接口 END-------------------------------------


    # ------------------------------- 数据处理类接口 START ----------------------------------
    /**
     * 复制用户到子库
     * @param int  $origin_db_id 来源数据库id
     *             $goal_db_ids  子库ids
     *             $copy_users   复制的病例
     * @author MSQ 2021-8-5
    **/
    public function copyUserToChildDb($origin_db_id = 0,$goal_db_ids = [],$copy_users = [],$usercare_id = 0)
    {
        if (!$origin_db_id || !$goal_db_ids || !$copy_users) return false;

        $copyUserIds = array_column($copy_users,'user_id');
        $i = 0;
        $addData = [];
        # 循环需要复制的子库
        foreach ($goal_db_ids as $child_k => $child_v) {
            # 当前子库已复制的病例
            $existUser = $this->getCopyUsers($child_v,$copyUserIds);
            $existUserIds = array_column($existUser,NULL,'user_id');

            # 循环需要复制的用户
            foreach ($copy_users as $user_k => $user_v) {
                if (!isset($existUserIds[$user_v['user_id']])) {
                    $addData[$i] = [
                        'user_id'      => $user_v['user_id'],
                        'usercare_id'  => $usercare_id,
                        'origin_db_id' => $origin_db_id,
                        'goal_db_id'   => $child_v,
                        'user_number'  => $user_v['user_number'],
                    ];
                    ++$i;
                }
            }
        }
        if ($addData) {
            $addData = array_chunk($addData,1000);
            foreach ($addData as $key => $value) {
                // $addRes = $this->saveAll($value);
                $addRes = $this->setAttr('id',null)->isUpdate(false)->saveAll($value);
                if (!$addRes) return false;
            }
        }
        return true;
    }
    /**
     * 删除子库复制用户
     * @param int  $origin_db_id 总库id
     * @param int  $goal_db_id 子库id 0表示总库删除该总库所有子库复制的delUserIds
     * @param int  $delUserIds 删除的用户id
     * @author MSQ 2021-8-7
    **/
    public function deleteChildDbUser($origin_db_id = 0,$goal_db_id = 0,$delUserIds = [])
    {
        if (!$origin_db_id && !$goal_db_id && !$delUserIds) return false;
        $where = [
            'status' => 1,
        ];
        if ($origin_db_id) $where['origin_db_id'] = $origin_db_id;
        if ($delUserIds) $where['user_id'] = ['IN',$delUserIds];
        if ($goal_db_id) $where['goal_db_id'] = $goal_db_id;
        
        return $this->update(['status' => 0],$where);
    }
    # --------------------------------- 数据处理类接口 END ----------------------------------

    
}
